import Vue from 'vue'
import App from './App.vue'
import router from './router'
import './style/comment.css'
import Element from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import '@/assets/font/font.css'
import store from './store'
Vue.config.productionTip = false
Vue.use(Element)

// webSocket管理

Vue.prototype.$webSocket = {
  socket: null,
  connect: function (url) {
    if (!this.socket) {
      this.socket = new WebSocket(url)

      this.socket.onopen = () => {
        console.log('连接成功')
      }

      this.socket.onmessage = (event) => {
        const obj = JSON.parse(event.data)
        store.commit('message/addMsg', obj)
      }

      this.socket.onerror = (error) => {
        console.error('连接出错', error)
      }

      this.socket.onclose = () => {
        this.socket = null
      }
    }
  }
}
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
